package com.hicorp.segment.controller;


import com.hicorp.segment.pojo.po.Menu;
import com.hicorp.segment.pojo.po.RoleMenuRelation;
import com.hicorp.segment.pojo.po.User;
import com.hicorp.segment.pojo.po.UserRoleRelation;
import com.hicorp.segment.service.AuthorityService;
import com.hicorp.segment.service.BaseInterface;
import com.hicorp.segment.utils.ResultBean;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/system")
public class SystemController {
    private final BaseInterface baseInterface;

    private final AuthorityService authorityService;

    public SystemController(AuthorityService authorityService, BaseInterface baseInterface) {
        this.authorityService = authorityService;
        this.baseInterface = baseInterface;
    }


    @PostMapping("/{tableName}")
    public Object createRecord(@PathVariable String tableName, @RequestBody User user) {
//        System.out.println(tableName);
        return authorityService.register(user);
    }

    @PutMapping("/{tableName}/{id}")
    public Object updateRecord(@PathVariable String tableName, @PathVariable Long id, @RequestBody Object record) {
        return baseInterface.update(tableName, id, record);
    }


    @GetMapping("/selectAllMenuTree")
    public ResultBean<List<Menu>> selectAllMenuTree() {
        return authorityService.selectAllMenuTree();
    }

    @PostMapping("/setRoleMenuRelation")
    public ResultBean<Void> setRoleMenuRelation(@RequestBody List<RoleMenuRelation> roleMenuRelations) {
        return authorityService.setRoleMenuRelation(roleMenuRelations);
    }

    @PostMapping("/setUserRoleRelation")
    public ResultBean<Void> setUserRoleRelation(@RequestBody List<UserRoleRelation> userRoleRelations) {
        return authorityService.setUserRoleRelation(userRoleRelations);
    }

    @GetMapping("/selectMenuIdByRoleId/{roleId}")
    public ResultBean<List<Long>> selectMenuIdByRoleId(@PathVariable Long roleId) {
        return authorityService.selectMenuIdByRoleId(roleId);
    }

    @GetMapping("/selectRoleIdByUserId/{userId}")
    public ResultBean<List<Long>> selectRoleIdByUserId(@PathVariable Long userId) {
        return authorityService.selectRoleIdByUserId(userId);
    }
}
